package com.eva.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.eva.user.Util.UserUtil;
import com.eva.user.pojo.User;


/**
 * 验证用户登录信息
 */
@WebServlet("confirmUser")
public class ConfirmUser extends HttpServlet{
	
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		HttpSession session = req.getSession();
		String loginName = (String) session.getAttribute("loginName");
		String password = (String) session.getAttribute("password");
		try {
			if(session.isNew()){
				resp.sendRedirect("/evs03/jsp/home.jsp?isLogin=false");
				return ;
			}
			User user = UserUtil.confirmAndGetUser(loginName, password);
			if(null == user){//登录失败
				resp.sendRedirect("/evs03/jsp/error.jsp?error="+"帐号或密码错误，请重试！");
			}else{
				if(null == user.getUserId()){
					resp.sendRedirect("/evs03/jsp/error.jsp?error="+user.getUserIntroduce());
				}else{//用户验证通过，登录成功，跳转到首页
					resp.sendRedirect("/evs03/jsp/hom.jsp?isLogin=true&user="+user);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("验证用户登录信息发生异常："+e.getMessage());
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}
}
